﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Rizmar.RizmarDataSetTableAdapters;

namespace Rizmar
{
    public partial class PesquisaClienteForm : Rizmar.PesquisaBaseForm
    {
        private Form parent;
        
        public PesquisaClienteForm()
        {
            InitializeComponent();
            
        }

        public PesquisaClienteForm(LancamentoForm form)
        {
            InitializeComponent();
            parent = form;
        }

        public PesquisaClienteForm(ClienteForm form)
        {
            InitializeComponent();
            parent = form;
        }

        public PesquisaClienteForm(baixaNormal form)
        {
            InitializeComponent();
            parent = form;
        }

        private void PesquisaClienteForm_Load(object sender, EventArgs e)
        {
            radioButtonNome.Checked = true;
            textBoxBusca.Focus();
            this.clienteTableAdapter.Fill(this.rizmarDataSet.Cliente);
        }


        private void textBoxBusca_TextChanged(object sender, EventArgs e)
        {
           RizmarDataSet.ClienteDataTable cdt = new RizmarDataSet.ClienteDataTable();
           if (radioButtonNome.Checked)
               cdt = clienteTableAdapter.PesquisaPorNome(textBoxBusca.Text);
           else
           {
               if (CPFRadioButton.Checked)
               {
                   if (textBoxBusca.Text.Length == 3 || textBoxBusca.Text.Length == 7)
                       textBoxBusca.AppendText(".");
                   if (textBoxBusca.Text.Length == 11)
                       textBoxBusca.AppendText("-");
               }
               else
               {
                   if (textBoxBusca.Text.Length == 2 || textBoxBusca.Text.Length == 6)
                       textBoxBusca.AppendText(".");
                   if (textBoxBusca.Text.Length == 10)
                       textBoxBusca.AppendText("/");
                   if (textBoxBusca.Text.Length == 15)
                       textBoxBusca.AppendText("-");
               }
               cdt = clienteTableAdapter.PesquisaPorDocumento(textBoxBusca.Text);
           }
            clienteBindingSource.DataSource = cdt;

        }

        private void radioButtonDocumento_CheckedChanged(object sender, EventArgs e)
        {
            RizmarDataSet.ClienteDataTable cdt = new RizmarDataSet.ClienteDataTable();
            if (radioButtonNome.Checked)
                cdt = clienteTableAdapter.PesquisaPorNome(textBoxBusca.Text);
            else
                cdt = clienteTableAdapter.PesquisaPorDocumento(textBoxBusca.Text);
            clienteBindingSource.DataSource = cdt;
            textBoxBusca.Focus();
        }

        private void buttonOK_Click(object sender, EventArgs e)
        {
            if (dataGridView.SelectedRows.Count > 0)
            {
                if (parent is LancamentoForm)
                {
                    (parent as LancamentoForm).textBoxCliente.Text = dataGridView.SelectedRows[0].Cells[1].Value.ToString();
                    (parent as LancamentoForm).textBoxCódigo.Text = dataGridView.SelectedRows[0].Cells[0].Value.ToString();
                }
                if (parent is ClienteForm)
                {
                    RizmarDataSet.TelefoneClienteDataTable tcdt = new RizmarDataSet.TelefoneClienteDataTable();
                    TelefoneClienteTableAdapter tcta = new TelefoneClienteTableAdapter();
                    (parent as ClienteForm).idTextBox.Text = dataGridView.SelectedRows[0].Cells[0].Value.ToString();
                    (parent as ClienteForm).nomeTextBox.Text = dataGridView.SelectedRows[0].Cells[1].Value.ToString();
                    (parent as ClienteForm).documentoTextBox.Text = dataGridView.SelectedRows[0].Cells[2].Value.ToString();
                    (parent as ClienteForm).emailTextBox.Text = dataGridView.SelectedRows[0].Cells[3].Value.ToString();
                    (parent as ClienteForm).logradouroTextBox.Text = dataGridView.SelectedRows[0].Cells[6].Value.ToString();
                    (parent as ClienteForm).numeroTextBox.Text = dataGridView.SelectedRows[0].Cells[7].Value.ToString();
                    (parent as ClienteForm).bairroTextBox.Text = dataGridView.SelectedRows[0].Cells[8].Value.ToString();
                    (parent as ClienteForm).cEPTextBox.Text = dataGridView.SelectedRows[0].Cells[9].Value.ToString();
                    (parent as ClienteForm).cidadeTextBox.Text = dataGridView.SelectedRows[0].Cells[10].Value.ToString();
                    (parent as ClienteForm).estadoTextBox.Text = dataGridView.SelectedRows[0].Cells[11].Value.ToString();
                    (parent as ClienteForm).limiteTextBox.Text = dataGridView.SelectedRows[0].Cells[12].Value.ToString();
                    (parent as ClienteForm).taxaTextBox.Text = dataGridView.SelectedRows[0].Cells[13].Value.ToString();
                    (parent as ClienteForm).dTextBox.Text = dataGridView.SelectedRows[0].Cells[14].Value.ToString();
                    (parent as ClienteForm).admTextBox.Text = dataGridView.SelectedRows[0].Cells[15].Value.ToString();
                    (parent as ClienteForm).duplicataTextBox.Text = dataGridView.SelectedRows[0].Cells[16].Value.ToString();
                    (parent as ClienteForm).obsTextBox.Text = dataGridView.SelectedRows[0].Cells[17].Value.ToString();
                    (parent as ClienteForm).complementoTextBox.Text = dataGridView.SelectedRows[0].Cells[18].Value.ToString();
                    (parent as ClienteForm).setStatus(ClienteForm.StatusClienteCadastro.editando);
                    (parent as ClienteForm).idTextBox.Enabled = false;

                    tcdt = tcta.PesquisaPorClienteID(Convert.ToInt32(dataGridView.SelectedRows[0].Cells[0].Value));
                    if (tcdt.Rows.Count > 0)
                    {
                        int i = 0;
                        (parent as ClienteForm).telefoneComboBox.Items.Clear();
                        (parent as ClienteForm).contatoComboBox.Items.Clear();
                        while (i< tcdt.Rows.Count)
                        {
                            (parent as ClienteForm).telefoneComboBox.Items.Add(tcdt.Rows[i]["telefone"]);
                            (parent as ClienteForm).contatoComboBox.Items.Add(tcdt.Rows[i]["contato"]);
                            i++;
                        }
                        (parent as ClienteForm).telefoneComboBox.Text = (parent as ClienteForm).telefoneComboBox.Items[0].ToString();
                        (parent as ClienteForm).contatoComboBox.Text = (parent as ClienteForm).contatoComboBox.Items[0].ToString();
                    }
                }
                this.Close();
                System.Windows.Forms.SendKeys.Send("{TAB}");
                System.Windows.Forms.SendKeys.Send("{TAB}");
                System.Windows.Forms.SendKeys.Send("{TAB}");
            }
            
        }

    

        private void dataGridView_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar.Equals(Convert.ToChar(Keys.Enter)))
                buttonOK_Click(sender,e);
        }

        private void novoButton_Click(object sender, EventArgs e)
        {
            if (parent is ClienteForm)
                if ((parent as ClienteForm).getStatus() == ClienteForm.StatusClienteCadastro.novoCliente) 
                    return;
                
            
            ClienteForm janela = new ClienteForm(this);
            janela.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
            janela.ShowDialog();
        }

    }
}
